import 'package:flutterstudyapplast/business/models/article_collect_item.dart';
import 'package:flutterstudyapplast/business/models/banner_item.dart';
import 'package:flutterstudyapplast/services/api/api.dart';
import 'package:dio/dio.dart';
import '../http_helper.dart';
import '../http_manager.dart';

class ApiImpl extends Api{

  static const String baseUrl = "https://www.wanandroid.com/";
  //首页文章列表 http://www.wanandroid.com/article/list/0/json
  static const String ARTICLE_LIST = "article/list/";
  static const String BANNER = "banner/json";
  //登录
  static const String LOGIN = "user/login";
  //注册
  static const String REGISTER = "user/register";
  //退出
  static const String LOGOUT = "user/logout/json";
  //收藏列表
  static const String COLLECT_ARTICLE_LIST = "lg/collect/list/";
  static const String COLLECT_WEBSITE_LIST = "lg/collect/usertools/json";
  //收藏站内
  static const String COLLECT_INTERNAL_ARTICLE = "lg/collect/";
  //取消收藏
  static const String UNCOLLECT_INTERNAL_ARTICLE = "lg/uncollect_originId/";
  static const String COLLECT_WEBSITE = "lg/collect/addtool/json";
  static const String UNCOLLECT_WEBSITE = "lg/collect/deletetool/json";
  static HttpManager http;

  static init() async {
    http = HttpManager.getInstance();
    await http.init();
  }


  @override
  Future<Map<String,dynamic>> getArticleList(int page) async{
    // TODO: implement getArticleList
    return await http.request('$ARTICLE_LIST$page/json');
  }

  @override
  Future<List<BannerItem>> getBanner() async{
    // TODO: implement getBanner
    var result = await http.request(BANNER);
    return HttpHelper.createBannerList(result);
  }

  @override
  Future<Map<String,dynamic>> getArticleCollects(int page) async{
    var result = await http.request("$COLLECT_ARTICLE_LIST/$page/json");
    return HttpHelper.createArticleCollects(result);
  }

  @override
  Future<List<ArticleCollctItem>> getWebsiteCollects() async{
    // TODO: implement getWebsiteCollects
    var result = await http.request(COLLECT_WEBSITE_LIST);
    return HttpHelper.createArticleWebsiteLista(result);
  }

  @override
  Future<Map<String,dynamic>> collectWebsite(String name, String url) async{
    // TODO: implement collectWebsite
    var formData = FormData.fromMap({"name": name, "link": url});
    return await http.request(COLLECT_WEBSITE, data: formData, method: "post");
  }

  @override
  Future<Map<String, dynamic>> unCollectWebsite(int id) async{
    // TODO: implement unCollectWebsite
    var formData = FormData.fromMap({"id": id});
    return await http.request(UNCOLLECT_WEBSITE, data: formData, method: "post");
  }

  @override
  Future<Map<String, dynamic>> collectArticle(int id) async{
    // TODO: implement collectArticle
    return await http.request("$COLLECT_INTERNAL_ARTICLE$id/json", method: "post");
  }

  @override
  Future<Map<String, dynamic>> unCollectArticle(int id) async{
    // TODO: implement unCollectArticle
    return await http.request("$UNCOLLECT_INTERNAL_ARTICLE$id/json", method: "post");
  }

  @override
  Future<Map<String, dynamic>> login(String username, String password) async{
    // TODO: implement login
    var formData = FormData.fromMap({
      "username": username,
      "password": password,
    });
    return await http.request(LOGIN, data: formData, method: "post");
  }

  @override
  Future<Map<String, dynamic>> register(String username, String password) async{
    // TODO: implement register
    ///必须使用form表单提交
    var formData = FormData.fromMap(
        {"username": username, "password": password, "repassword": password});
    return await http
        .request(REGISTER, data: formData, method: "post");
  }
  static clearCookie() {
    http.clearCookie();
  }
}